﻿using Prism.Events;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PrismEventAggregator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow(IEventAggregator eventAggregator)
        {
            InitializeComponent();

            // 1、获取消息总线对象：通过注入的方式获取
            // 2、通过消息对象进行订阅或者发布

            //总线   事件对象
            #region 消息订阅
            // (1)订阅无参
            eventAggregator
                .GetEvent<EventMessage>()
                .Subscribe(Receive);

            // 全局静态处理   就可以不使用构造函数注入
            // 订阅带参
            // (2)多个参数就封装成一个类
            Messenger.Defualt.GetEvent<EventMessageArgs>()
                .Subscribe(Receive);
            #endregion

            #region 消息发布  A类
            eventAggregator.GetEvent<EventMessage>()
               .Publish();

            //Messenger.Defualt.GetEvent<EventMessageArgs>()
            //    .Publish("Hello");

            // 1、先执行这里   发布
            Messenger.Defualt.GetEvent<EventMessageArgs>()
                .Publish(new EventAction()
                {
                    // 3、后面还有逻辑
                    ResultAction = new Action<bool>(state =>
                    {
                        // .......
                    })
                });


            // 线程发布
            // 主线程：UI线程
            Task.Run(() =>
            {
                Messenger.Defualt.GetEvent<EventMessageArgs1>()
                    .Publish(new DataModel { Id = 1, Text = "Hello" });
                Messenger.Defualt.GetEvent<EventMessageArgs1>()
                    .Publish(new DataModel { Id =2, Text = "Hello" });
            });
            #endregion
        }


        // 无参数触发
        private void Receive()
        {

        }
        // 带参数触发
        private void Receive(object arg)
        {

        }
    }
}